//
//  MqForumModuleViewController.m
//  Meiqi
//
//  Created by lanou on 16/1/12.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqForumModuleViewController.h"
#import "MqForumModule.h"
#import "MqForumModuleDetailViewController.h"
@interface MqForumModuleViewController ()

<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) NSDictionary *dataDic;

@property (nonatomic, strong) UICollectionView *collectView;

@end

@implementation MqForumModuleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"版块列表";
    
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    
    [self dictionaryBulid];
    
    [self addCollectionView];
    
}

- (void)addCollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    //  行间距
    layout.minimumLineSpacing = 10;
    
    //  列间距
    layout.minimumInteritemSpacing = 10;
    
    //  item大小
    layout.itemSize = CGSizeMake((kScreenWidth - 50) / 4, 20);
    
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //  滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //  表头
    layout.headerReferenceSize = CGSizeMake(kScreenWidth, 30);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) collectionViewLayout:layout];
    
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    //  默认背景黑色
    collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:collectionView];
    
    //  注册
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
}

#pragma mark --- 数据源处理
- (void)dictionaryBulid{
    
    _dataDic = [NSDictionary dictionary];
    
    MqForumModule *model1 = [[MqForumModule alloc] initWithTitle:@"有问必答" fid:@"243"];
    MqForumModule *model2 = [[MqForumModule alloc] initWithTitle:@"美粉杂谈" fid:@"66"];
    MqForumModule *model3 = [[MqForumModule alloc] initWithTitle:@"美骑达人" fid:@"441"];
    MqForumModule *model4 = [[MqForumModule alloc] initWithTitle:@"车友驿站" fid:@"90"];
    MqForumModule *model5 = [[MqForumModule alloc] initWithTitle:@"骑行故事" fid:@"72"];
    MqForumModule *model6 = [[MqForumModule alloc] initWithTitle:@"骑行攻略" fid:@"70"];
    MqForumModule *model7 = [[MqForumModule alloc] initWithTitle:@"骑行摄影" fid:@"67"];
    MqForumModule *model8 = [[MqForumModule alloc] initWithTitle:@"骑行安全" fid:@"277"];
    MqForumModule *model9 = [[MqForumModule alloc] initWithTitle:@"骑行健康" fid:@""];
    MqForumModule *model10 = [[MqForumModule alloc] initWithTitle:@"女子骑行" fid:@"91"];
    MqForumModule *model11 = [[MqForumModule alloc] initWithTitle:@"活动发布" fid:@"408"];
    MqForumModule *model12 = [[MqForumModule alloc] initWithTitle:@"综合讨论" fid:@"71"];
    MqForumModule *model13 = [[MqForumModule alloc] initWithTitle:@"山地车" fid:@"255"];
    MqForumModule *model14 = [[MqForumModule alloc] initWithTitle:@"公路车" fid:@"256"];
    MqForumModule *model15 = [[MqForumModule alloc] initWithTitle:@"折叠车" fid:@"92"];
    MqForumModule *model16 = [[MqForumModule alloc] initWithTitle:@"旅行车" fid:@"258"];
    MqForumModule *model17 = [[MqForumModule alloc] initWithTitle:@"HPV躺车" fid:@"74"];
    MqForumModule *model18 = [[MqForumModule alloc] initWithTitle:@"二手交易" fid:@"39"];
    MqForumModule *model19 = [[MqForumModule alloc] initWithTitle:@"美骑易购" fid:@"404"];
    MqForumModule *model20 = [[MqForumModule alloc] initWithTitle:@"赛事预告" fid:@"253"];
    MqForumModule *model21 = [[MqForumModule alloc] initWithTitle:@"赛事分享" fid:@"443"];
    MqForumModule *model22 = [[MqForumModule alloc] initWithTitle:@"美骑车友会" fid:@"444"];
    MqForumModule *model23 = [[MqForumModule alloc] initWithTitle:@"车队" fid:@"445"];
    MqForumModule *model24 = [[MqForumModule alloc] initWithTitle:@"校园俱乐部" fid:@"447"];
    MqForumModule *model25 = [[MqForumModule alloc] initWithTitle:@"中老年车友" fid:@"69"];
    MqForumModule *model26 = [[MqForumModule alloc] initWithTitle:@"社区事务" fid:@"54"];

    _dataDic = @{@"车友":@[model1, model2, model3, model4],
                 @"骑行":@[model5, model6, model7, model8, model9, model10, model11],
                 @"产品":@[model12, model13, model14, model15, model16, model17, model18, model19],
                 @"赛事":@[model20, model21],
                 @"聚乐":@[model22, model23, model24, model25],
                 @"管理中心":@[model26]
                 };
    
}

#pragma mark --- colletiobView代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    NSArray *keys = _dataDic.allKeys;
    
    NSArray *values = _dataDic[keys[section]];
    
    return values.count;
    
    
}

//  返回分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return _dataDic.count;
    
}

//  分区表头

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
        //  表头
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:headerView.bounds];
    titleLabel.font = [UIFont systemFontOfSize:12];
    [headerView addSubview:titleLabel];

    NSArray *keys = _dataDic.allKeys;

    titleLabel.text = keys[indexPath.section];

    return headerView;
    
}


//  返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor redColor];
    
    //  从字典中取出对应的model
    NSArray *keys = _dataDic.allKeys;
    
    NSString *key = keys[indexPath.section];
    
    NSArray *values = _dataDic[key];
    
    MqForumModule *module = values[indexPath.row];
    
    UILabel *label = [[UILabel alloc] initWithFrame:cell.bounds];
    label.text = module.title;
    label.backgroundColor = [UIColor lightGrayColor];
    label.layer.cornerRadius = 10;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:12];
    cell.backgroundView = label;
    cell.backgroundView.layer.masksToBounds = YES;
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MqForumModuleDetailViewController *moduleDetailVC = [[MqForumModuleDetailViewController alloc] init];
    
    //  取出对应cell的module
    NSArray *keys = _dataDic.allKeys;
    
    NSString *key = keys[indexPath.section];
    
    NSArray *values = _dataDic[key];
    
    MqForumModule *module = values[indexPath.row];
    
    //  初始化单例类 并添加
    MqManagerHandle *handle = [MqManagerHandle sharedManager];
    [handle addHistoryObjectWith:module];

    moduleDetailVC.fid = module.fid;
    
    [self.navigationController pushViewController:moduleDetailVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
